//
//  YMRegisterViewController.m
//  YMMedical
//
//  Created by y s on 2023/7/14.
//

#import "YMRegisterViewController.h"
#import "YMLoginInputBoxView.h"
#import "IQKeyboardManager.h"
#import "YMLoginService.h"



static NSString *timer_key = @"RegisterCodeTimer_Key";
@interface YMRegisterViewController ()<YMLoginInputBoxViewDelegate>

@property (nonatomic, strong) UIScrollView *svMain;
@property (nonatomic, strong) UIView *vContent;
@property (nonatomic, strong) UIImageView *imgvLogo;

@property (nonatomic, strong) YMLoginInputBoxView *vMobile;
@property (nonatomic, strong) YMLoginInputBoxView *vCode;
@property (nonatomic, strong) YMLoginInputBoxView *vPwd;
@property (nonatomic, strong) YMLoginInputBoxView *vAgainPwd;
@property (nonatomic, strong) UILabel *lblTips;

@property (nonatomic, strong) UIButton *btnAgree;
@property (nonatomic, strong) YYLabel *lblAgree;
@property (nonatomic, strong) UIButton *btnRegister;

@property (nonatomic, strong) UIButton *btnLogin;
@property (nonatomic, strong) UILabel *lblVersion;

@property (nonatomic, copy) NSString *strMobile;
@property (nonatomic, copy) NSString *strCode;
@property (nonatomic, copy) NSString *strPwd;
@property (nonatomic, copy) NSString *strAgainPwd;

@end

@implementation YMRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
}

#pragma mark -- UI
- (void)setUI{
    [self showNavTitle:KLanguage(@"register_txt")];
    [self.view addSubview:self.svMain];
    [self.svMain addSubview:self.vContent];
    [self.vContent addSubview:self.imgvLogo];
    [self.vContent addSubview:self.vMobile];
    [self.vContent addSubview:self.vCode];
    [self.vContent addSubview:self.vPwd];
    [self.vContent addSubview:self.vAgainPwd];
    [self.vContent addSubview:self.lblTips];
    
    [self.vContent addSubview:self.btnAgree];
    [self.vContent addSubview:self.lblAgree];
    [self.vContent addSubview:self.btnRegister];
    [self.vContent addSubview:self.btnLogin];
    [self.vContent addSubview:self.lblVersion];
    
    [self.svMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.vContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.svMain);
        make.width.mas_equalTo(SCREEN_WIDTH);
//        make.height.mas_greaterThanOrEqualTo(self.view.bounds.size.height);
    }];
    [self.imgvLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo([SFitUtil fitSize:90 height:90]);
        make.top.mas_equalTo([SFitUtil fit:30]);
    }];
    [self.vMobile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.height.mas_equalTo([SFitUtil fit:50]);
        make.top.equalTo(self.imgvLogo.mas_bottom).offset([SFitUtil fit:30]);
    }];
    [self.vCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.vMobile);
        make.top.equalTo(self.vMobile.mas_bottom).offset([SFitUtil fit:15]);
    }];
    [self.vPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.vMobile);
        make.top.equalTo(self.vCode.mas_bottom).offset([SFitUtil fit:15]);
    }];
    [self.lblTips mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.vMobile);
        make.left.mas_equalTo([SFitUtil fit:21]);
        make.top.equalTo(self.vPwd.mas_bottom).offset([SFitUtil fit:15]);
    }];
    [self.vAgainPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.vMobile);
        make.top.equalTo(self.lblTips.mas_bottom).offset([SFitUtil fit:15]);
    }];
    [self.btnAgree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.vAgainPwd.mas_bottom).offset([SFitUtil fit:15]);
        make.height.mas_equalTo([SFitUtil fit:34]);
        make.width.mas_equalTo([SFitUtil fit:12+14]);
        make.left.mas_equalTo([SFitUtil fit:9]);
    }];
    [self.lblAgree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.btnAgree.mas_right);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.centerY.equalTo(self.btnAgree);
    }];
    [self.btnRegister mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btnAgree.mas_bottom).offset([SFitUtil fit:15]);
        make.height.mas_equalTo([SFitUtil fit:54]);
        make.width.mas_equalTo([SFitUtil fit:345]);
        make.centerX.mas_equalTo(0);
    }];
    [self.btnLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.greaterThanOrEqualTo(self.btnRegister.mas_bottom).offset([SFitUtil fit:30]);
        make.size.mas_equalTo([SFitUtil fitSize:168 height:50]);
        make.bottom.mas_equalTo(-IPHONE_X_BOTTOM_HEIGHT-[SFitUtil fit:38]);
        make.centerX.mas_equalTo(0);
    }];
    [self.lblVersion mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(self.btnLogin.mas_bottom).offset([SFitUtil fit:12]);
    }];
    
    self.strMobile = self.phoneStr;
    self.vMobile.strInput = self.phoneStr;
    
}


#pragma mark -- event action
- (void)registerClickAction:(id)sender{
   //注册
    if (self.strMobile == nil || _strMobile.length < 1) {
        [SHUD showInfo:KLanguage(@"input_phone_txt")];
        return;
    }
    if (self.strMobile.length < 11) {
        [SHUD showInfo:KLanguage(@"input_right_phone_txt")];
        return;
    }
    if (self.strCode == nil || self.strCode.length < 1) {
        [SHUD showInfo:KLanguage(@"input_sms_code_txt")];
        return;
    }
    if (self.strCode.length < SMS_number_key) {
        [SHUD showInfo:KLanguage(@"input_right_sms_code_txt")];
        return;
    }
    if (self.strPwd == nil || self.strPwd.length < 1) {
        [SHUD showInfo:KLanguage(@"input_pwd_txt")];
        return;
    }
    if (self.strAgainPwd == nil || self.strAgainPwd.length < 1) {
        [SHUD showInfo:KLanguage(@"re_input_pwd_txt")];
        return;
    }
//    if (self.strPwd.length < 8 || self.strPwd.length > 16) {
//        [SHUD showInfo:KLanguage(@"re_input_right_pwd_txt")];
//        return;
//    }
//    if (self.strAgainPwd.length < 8 || self.strAgainPwd.length > 16 ) {
//        [SHUD showInfo:KLanguage(@"re_input_right_pwd_again_txt")];
//        return;
//    }
    if ([self.strPwd isEqualToString:self.strAgainPwd] == NO) {
        [SHUD showInfo:KLanguage(@"pwd_not_same_txt")];
        return;
    }
    if (self.btnAgree.isSelected == NO) {
        [SHUD showInfo:KLanguage(@"read_agree_ag_txt")];
        return;
    }
    WEAK_SELF;
    [YMLoginService requestUserRegisterWithPhone:self.strMobile password:self.strPwd confirmPwd:self.strAgainPwd code:self.strCode success:^{
        [SHUD showInfo:@"注册成功"];
        [YMBridging goAppTabWithIndex:0];
        [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSString * _Nonnull message) {
        [SHUD showInfo:message];
    }];

    
}
- (void)clickAgreeUrl:(NSInteger)type{
    //跳转协议
    [YMBridging goToProtocolVC:type];
}
- (void)agreeAction:(id)sender{
    self.btnAgree.selected = !self.btnAgree.isSelected;
}
- (void)loginClickAction:(id)sender{
    //登录
    [SJumpUtil popVC:self animated:YES];
}

#pragma mark -- YMLoginInputBoxViewDelegate
- (void)textFiledChanged:(UITextField *)sender{
    if (sender.tag == 200) {
        if (sender.text.length > 11) {
            sender.text = [sender.text substringToIndex:11];
        }
        self.strMobile = sender.text;
    }else if (sender.tag == 201){
        if (sender.text.length > SMS_number_key) {
            sender.text = [sender.text substringToIndex:SMS_number_key];
        }
        self.strCode = sender.text;
    }else if (sender.tag == 202){
        if (sender.text.length > 16) {
            sender.text = [sender.text substringToIndex:16];
        }
        self.strPwd = sender.text;
    }else if (sender.tag == 203){
        if (sender.text.length > 16) {
            sender.text = [sender.text substringToIndex:16];
        }
        self.strAgainPwd = sender.text;
    }
    
}
- (void)getMoboleCode:(UIButton *)sender{
    //获取验证码
    if (self.strMobile == nil || _strMobile.length < 1) {
        [SHUD showInfo:KLanguage(@"input_phone_txt")];
        return;
    }
    if (self.strMobile.length < 11) {
        [SHUD showInfo:KLanguage(@"input_right_phone_txt")];
        return;
    }
    sender.userInteractionEnabled = NO;
    WEAK_SELF;
    [YMLoginService requestUserSendCodeWithPhone:self.strMobile success:^{
        [weakSelf startTimer:sender];
    } failure:^(NSString * _Nonnull message) {
        [SHUD showInfo:message];
        sender.userInteractionEnabled = YES;
    }];
}

- (void)startTimer:(UIButton *)sender{
    [sender setTitleColor:STheme.D4 forState:UIControlStateNormal];
    __block NSInteger allSecond = 60;
    [STimeUtil startTimer:timer_key second:1 action:^{
        allSecond -= 1;
        [sender setTitle:[NSString stringWithFormat:@"%ldS %@",allSecond,KLanguage(@"resend_format_txt")] forState:UIControlStateNormal];
        if (allSecond == 0) {
            [STimeUtil stopTimer:timer_key];
            sender.userInteractionEnabled = YES;
            [sender setTitle:KLanguage(@"resend_format_txt") forState:UIControlStateNormal];
            [sender setTitleColor:STheme.D12 forState:UIControlStateNormal];
        }
    }];
}

#pragma mark -- setter and getter
- (UIScrollView *)svMain{
    if (_svMain == nil) {
        _svMain = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _svMain.showsVerticalScrollIndicator = NO;
        _svMain.showsHorizontalScrollIndicator = NO;
        _svMain.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    return _svMain;
}
- (UIView *)vContent{
    if (_vContent == nil) {
        _vContent = [[UIView alloc] init];
    }
    return _vContent;
}
- (UIImageView *)imgvLogo{
    if (_imgvLogo == nil) {
        _imgvLogo = [[UIImageView alloc] init];
        _imgvLogo.image = [UIImage imageNamed:@"tools_app_logo_icon"];
    }
    return _imgvLogo;
}
- (YMLoginInputBoxView *)vMobile{
    if (_vMobile == nil) {
        _vMobile = [[YMLoginInputBoxView alloc] init];
        _vMobile.strTitle = KLanguage(@"phone_txt");
        _vMobile.strPlace = KLanguage(@"input_txt");
        _vMobile.delegate = self;
        _vMobile.tfTag = 200;
        _vMobile.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _vMobile;
}
- (YMLoginInputBoxView *)vCode{
    if (_vCode == nil) {
        _vCode = [[YMLoginInputBoxView alloc] init];
        _vCode.strTitle = KLanguage(@"sms_code_txt");
        _vCode.strPlace = KLanguage(@"input_txt");
        _vCode.delegate = self;
        _vCode.tfTag = 201;
        _vCode.keyboardType = UIKeyboardTypeNumberPad;
        _vCode.showBtn = YES;
        _vCode.strBtn = KLanguage(@"get_code_txt");
        _vCode.tfAlignment = NSTextAlignmentLeft;
    }
    return _vCode;
}
- (YMLoginInputBoxView *)vPwd{
    if (_vPwd == nil) {
        _vPwd = [[YMLoginInputBoxView alloc] init];
        _vPwd.strTitle = KLanguage(@"password_txt");
        _vPwd.strPlace = KLanguage(@"input_pwd_txt");
        _vPwd.delegate = self;
        _vPwd.tfTag = 202;
        _vPwd.isPwd = YES;
        _vPwd.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    }
    return _vPwd;
}
- (YMLoginInputBoxView *)vAgainPwd{
    if (_vAgainPwd == nil) {
        _vAgainPwd = [[YMLoginInputBoxView alloc] init];
        _vAgainPwd.strTitle = KLanguage(@"confirm_pwd_txt");
        _vAgainPwd.strPlace = KLanguage(@"re_input_pwd_txt");
        _vAgainPwd.delegate = self;
        _vAgainPwd.tfTag = 203;
        _vAgainPwd.isPwd = YES;
        _vAgainPwd.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    }
    return _vAgainPwd;
}
- (UILabel *)lblTips{
    if (_lblTips == nil) {
        _lblTips = [SThemeUtil label:@"" color:0x999999 fontSize:13];
        NSString *allStr = [NSString stringWithFormat:@"*%@",KLanguage(@"pwd_format_txt")];
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:allStr attributes:@{NSFontAttributeName:[SFitUtil fitFont:13], NSForegroundColorAttributeName: STheme.D4}];
        [attr addAttributes:@{NSForegroundColorAttributeName: [SUIUtil colorOf:0xFF3141]} range:NSMakeRange(0, 1)];
        _lblTips.attributedText = attr;
        _lblTips.preferredMaxLayoutWidth = SCREEN_WIDTH - [SFitUtil fit:21]-[SFitUtil fit:15];
        _lblTips.numberOfLines = 0;
    }
    return _lblTips;
}
- (YYLabel *)lblAgree{
    if (!_lblAgree) {
        _lblAgree = [[YYLabel alloc] init];
        _lblAgree.textColor = STheme.D4;
        _lblAgree.font = [SFitUtil fitFont:14];
        _lblAgree.textAlignment = NSTextAlignmentLeft;
        _lblAgree.numberOfLines = 0;
        _lblAgree.preferredMaxLayoutWidth = SCREEN_WIDTH-[SFitUtil fit:35+15];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:[SFitUtil fit:4]];
        NSDictionary *attDict = @{NSParagraphStyleAttributeName:paragraphStyle};
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:KLanguage(@"readed_agreed_txt") attributes:attDict];
        NSString *readText1 = [NSString stringWithFormat:@"%@%@",KLanguage(@"ag_hint_2_txt"),KLanguage(@"and_txt")];
        WEAK_SELF;
        NSMutableAttributedString *read1 = [[NSMutableAttributedString alloc] initWithString:readText1];
        [read1 setTextHighlightRange:NSMakeRange(0, readText1.length) color:STheme.D12 backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            [weakSelf clickAgreeUrl:1];
        }];
        [att appendAttributedString:read1];
        
        NSString *readText2 = KLanguage(@"ag_hint_3_txt");
        NSMutableAttributedString *read2 = [[NSMutableAttributedString alloc] initWithString:readText2];
        [read2 setTextHighlightRange:NSMakeRange(0, readText2.length) color:STheme.D12 backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            [weakSelf clickAgreeUrl:2];
        }];
        [att appendAttributedString:read2];
        _lblAgree.attributedText = att;
    }
    return _lblAgree;
}
- (UIButton *)btnAgree {
    if (_btnAgree == nil) {
        _btnAgree = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnAgree setImage:[UIImage imageNamed:@"login_agreement_normal"] forState:UIControlStateNormal];
        [_btnAgree setImage:[UIImage imageNamed:@"login_agreement_selected"] forState:UIControlStateSelected];
        [_btnAgree addTarget:self action:@selector(agreeAction:) forControlEvents:UIControlEventTouchUpInside];
        [_btnAgree g_clickEdgeWithTop:10 bottom:10 left:10 right:10];
    }
    return _btnAgree;
}
- (UIButton *)btnRegister{
    if (_btnRegister == nil) {
        _btnRegister = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnRegister.titleLabel.font = [SFitUtil fitBoldFont:18];
        [_btnRegister setTitleColor:STheme.D9 forState:UIControlStateNormal];
        [_btnRegister setTitle:KLanguage(@"register_txt") forState:UIControlStateNormal];
        [_btnRegister addTarget:self action:@selector(registerClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnRegister.clipsToBounds = YES;
        _btnRegister.layer.cornerRadius = [SFitUtil fit:8];
        [_btnRegister setBackgroundImage:[UIImage imageWithColor:STheme.D12] forState:UIControlStateSelected];
        [_btnRegister setBackgroundImage:[UIImage imageWithColor:[SUIUtil colorOf:STheme.H_D12 alpha:0.5]] forState:UIControlStateNormal];
        _btnRegister.selected = YES;
    }
    return _btnRegister;
}
- (UIButton *)btnLogin{
    if (_btnLogin == nil) {
        _btnLogin = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnLogin.titleLabel.font = [SFitUtil fitFont:16];
        [_btnLogin setTitleColor:STheme.D12 forState:UIControlStateNormal];
        [_btnLogin setTitle:KLanguage(@"have_account_login_txt") forState:UIControlStateNormal];
        [_btnLogin addTarget:self action:@selector(loginClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnLogin.clipsToBounds = YES;
        _btnLogin.layer.cornerRadius = [SFitUtil fit:25];
        _btnLogin.backgroundColor = STheme.D9;
    }
    return _btnLogin;
}
- (UILabel *)lblVersion{
    if (_lblVersion == nil){
        _lblVersion = [SThemeUtil label:@"" color:STheme.H_D12 fontSize:14];
        _lblVersion.text = @"Vi 1";
    }
    return _lblVersion;
}

- (void)dealloc{
    [STimeUtil stopTimer:timer_key];
}


@end
